<template>
  <el-dialog
    title="推荐商品"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="120px" @keyup.enter.native="dataFormSubmit()">
      <el-row>
        <el-col :span="24">
          <el-form-item label="推荐开始日期" prop="adstartdate">
            <el-date-picker
              v-model="dataForm.startDate"
              style="width: 100%"
              type="datetime"
              placeholder="选择日期时间"
              align="right"
              :picker-options="pickerOptions"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="推荐结束日期" prop="adstartdate">
            <el-date-picker
              v-model="dataForm.endDate"
              style="width: 100%"
              type="datetime"
              placeholder="选择日期时间"
              align="right"
              :picker-options="pickerOptions"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col style="text-align: right;">
          <el-button size="mini" icon="el-icon-close" @click="visible = false">取消</el-button>
          <el-button size="mini" icon="el-icon-check" type="primary" @click="dataFormSubmit()">确定</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>

</template>
<script>
export default {
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date())
          }
        }, {
          text: '明天',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() + 3600 * 1000 * 24 * 1)
            picker.$emit('pick', date)
          }
        }, {
          text: '一周后',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() + 3600 * 1000 * 24 * 7)
            picker.$emit('pick', date)
          }
        }, {
          text: '二周后',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() + 3600 * 1000 * 24 * 14)
            picker.$emit('pick', date)
          }
        }, {
          text: '三周后',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() + 3600 * 1000 * 24 * 21)
            picker.$emit('pick', date)
          }
        }]
      },
      dataRule: {
        adpositionid: [
          { required: true, message: '位置id不能为空', trigger: 'blur' }
        ]
      },
      visible: false,
      dataForm: {
        type: '',
        page: '',
        ids: [],
        startDate: '',
        endDate: ''
      }
    }
  },
  methods: {
    init(ids, page, type) {
      this.visible = true
      this.dataForm.ids = ids
      this.type = type
      this.page = page
    },
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid && this.type) {
          this.$http({
            url: this.$http.adornUrl(`/goods/recommand/goods/save`),
            method: 'post',
            data: this.$http.adornData({
              ids: this.dataForm.ids,
              startDate: this.dataForm.startDate,
              endDate: this.dataForm.endDate,
              page: this.page,
              type: this.type
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
